<?php
    require_once('services/product.php');
    
    $productId = (int)$_GET['id'] or die('Param is not correct type'); 
    if(isset($_GET['action']) && $_GET['action']=='remove'){
        
        unset($_SESSION['cart'][$productId]);        
        return 'success';
    }
    
    $productService = new ProductService();
    $aRow = $productService -> getProduct($productId);
    
    if($aRow != null){
        //Check that Session cart is set or not
        //if not set new array and then insert new product in session
        //if yes just insert product
        if(!isset($_SESSION['cart'])){
            $_SESSION['cart'] = array();
        }
        if(isset($_SESSION['cart'][$aRow['id']])){
            $_SESSION['cart'][$aRow['id']]['soluong'] =  (int)$_SESSION['cart'][$aRow['id']]['soluong'] + 1;
        }else{
            $aRow['soluong'] = 1; //default is 1;
            $_SESSION['cart'][$aRow['id']] =  $aRow;    
        }
        echo count($_SESSION['cart']);
    }else{
        die('There\'s no entry for this id');
    }
?>